package cn.com.huak.www.sell.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 手动的方式获取微信的openid
 * @author huak@wanggang
 * @date 2019/1/6 16:21
 */
@RestController
@RequestMapping("/weixin")
@Slf4j
public class WeixinController {

    @GetMapping("/auth")
    public void auth(@RequestParam("code") String code){
        if(StringUtils.isEmpty(code)){
            throw new RuntimeException("微信网页授权返回的code值为空.....");
        }
        //定义获取access_token的路径
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token" +
                "?appid=wx66aaac238d4a3370&secret=c9bef1f2cc6ae69373668b36cfe992ea&" +
                "code=" + code + "&grant_type=authorization_code";
        log.info("进入微信授权方法code:{}", code);
        // 发送get请求获取access_token
        RestTemplate restTemplate = new RestTemplate();
        String forObject = restTemplate.getForObject(url, String.class);
        log.info("获取access_token的返回值:{}", forObject);
    }
}
